package com.googlecode.spruce.remote;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Entry point of the application
 */
public class Main
{
    /**
     * Entry point of the application
     * 
     * @param args
     *            Command line arguments
     * @throws ParseException
     */
    public static void main(final String[] args) throws ParseException
    {
        final Options options = new Options();

        final OptionGroup requiredOptions = new OptionGroup();
        requiredOptions.setRequired(true);
        requiredOptions.addOption(new Option("c", true, "Path to Spring XML context file"));

        options.addOptionGroup(requiredOptions);

        final CommandLineParser parser = new BasicParser();
        final CommandLine cmd = parser.parse(options, args);

        final String springContext = cmd.getOptionValue('c');

        /* Loads and starts the Spring context */
        final FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(springContext);

        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                synchronized (context)
                {
                    context.close();
                }
            }
        });
    }
}
